﻿Public Class FileManager

    Public Shared Function GetPhysicalPath(ByVal MobilePath As String) As String
        Return (MobilePath.URLDecode.TrimStart("/").Substring("My Mobile".Length) & "\").Replace("/", "\")
    End Function

    Public Shared Function CombinePath(ByVal Path1 As String, ByVal Path2 As String) As String
        Return Path1.TrimEnd("\") & "\" & Path2.TrimStart("\")
    End Function

    Public Function GetProperties(ByVal ParentFolder As String, ByVal Data As String, ByVal Type As String) As String
        Dim Path = GetPhysicalPath(CombinePath(ParentFolder, Data)).TrimEnd("\")
        Dim objProp As New FFProperties
        If Type.ToLower = "folder" Then
            Dim DirInfo As New IO.DirectoryInfo(Path)
            objProp.Attributes = DirInfo.Attributes.ToString
            objProp.CreationTime = DirInfo.CreationTime
            objProp.DirectoriesCount = DirInfo.GetDirectories.Count
            objProp.Extension = ""
            objProp.FilesCount = DirInfo.GetFiles.Count
            objProp.FullName = DirInfo.FullName
            objProp.LastAccessTime = DirInfo.LastAccessTime
            objProp.LastWriteTime = DirInfo.LastWriteTime
            objProp.Length = 0
            objProp.Data = Data
            objProp.Type = "Folder"
        Else
            Dim FileInfo As New IO.FileInfo(Path)
            objProp.Attributes = FileInfo.Attributes.ToString
            objProp.CreationTime = FileInfo.CreationTime
            objProp.DirectoriesCount = 0
            objProp.Extension = FileInfo.Extension
            objProp.FilesCount = 0
            objProp.FullName = FileInfo.FullName
            objProp.LastAccessTime = FileInfo.LastAccessTime
            objProp.LastWriteTime = FileInfo.LastWriteTime
            objProp.Length = FileInfo.Length
            objProp.Data = Data
            objProp.Type = "File"
        End If
        Return ToJson(objProp)
    End Function

    Private Class FFProperties

        Private _Attributes As String
        Public Property Attributes() As String
            Get
                Return _Attributes
            End Get
            Set(ByVal value As String)
                _Attributes = value
            End Set
        End Property

        Private _CreationTime As String
        Public Property CreationTime() As String
            Get
                Return _CreationTime
            End Get
            Set(ByVal value As String)
                _CreationTime = value
            End Set
        End Property

        Private _LastAccessTime As String
        Public Property LastAccessTime() As String
            Get
                Return _LastAccessTime
            End Get
            Set(ByVal value As String)
                _LastAccessTime = value
            End Set
        End Property

        Private _LastWriteTime As String
        Public Property LastWriteTime() As String
            Get
                Return _LastWriteTime
            End Get
            Set(ByVal value As String)
                _LastWriteTime = value
            End Set
        End Property

        Private _Extension As String
        Public Property Extension() As String
            Get
                Return _Extension
            End Get
            Set(ByVal value As String)
                _Extension = value
            End Set
        End Property

        Private _Length As Long
        Public Property Length() As Long
            Get
                Return _Length
            End Get
            Set(ByVal value As Long)
                _Length = value
            End Set
        End Property

        Private _FullName As String
        Public Property FullName() As String
            Get
                Return _FullName.Replace("\", "/")
            End Get
            Set(ByVal value As String)
                _FullName = value
            End Set
        End Property

        Private _DirectoriesCount As Long
        Public Property DirectoriesCount() As Long
            Get
                Return _DirectoriesCount
            End Get
            Set(ByVal value As Long)
                _DirectoriesCount = value
            End Set
        End Property

        Private _FilesCount As Long
        Public Property FilesCount() As Long
            Get
                Return _FilesCount
            End Get
            Set(ByVal value As Long)
                _FilesCount = value
            End Set
        End Property

        Private _Data As String
        Public Property Data() As String
            Get
                Return _Data
            End Get
            Set(ByVal value As String)
                _Data = value
            End Set
        End Property

        Private _Type As String
        Public Property Type() As String
            Get
                Return _Type
            End Get
            Set(ByVal value As String)
                _Type = value
            End Set
        End Property

        Private _Success As Boolean = True 'future implementation
        Public Property Success() As Boolean
            Get
                Return _Success
            End Get
            Set(ByVal value As Boolean)
                _Success = value
            End Set
        End Property

    End Class

    Public Function CreateFolder(ByVal ParentFolder As String, ByVal NewFolderName As String) As String
        Dim objGeneric As New GenericJson
        Try
            Dim PhysicalFolder = GetPhysicalPath(ParentFolder)
            PhysicalFolder = CombinePath(PhysicalFolder, NewFolderName)
            System.IO.Directory.CreateDirectory(PhysicalFolder)
            objGeneric.Success = True
        Catch ex As Exception
            objGeneric.Success = False
            objGeneric.Msg = ex.Message
        End Try
        Return objGeneric.ToString
    End Function

    Public Function DeleteFolder(ByVal ParentFolder As String, ByVal FolderName As String) As String
        Dim objGeneric As New GenericJson
        Try
            Dim PhysicalFolder = GetPhysicalPath(ParentFolder)
            PhysicalFolder = CombinePath(PhysicalFolder, FolderName)
            System.IO.Directory.Delete(PhysicalFolder, True)
            objGeneric.Success = True
        Catch ex As Exception
            objGeneric.Success = False
            objGeneric.Msg = ex.Message
        End Try
        Return objGeneric.ToString
    End Function

    Public Function DeleteFile(ByVal ParentFolder As String, ByVal FileName As String) As String
        Dim objGeneric As New GenericJson
        Try
            Dim PhysicalFolder = GetPhysicalPath(ParentFolder)
            PhysicalFolder = CombinePath(PhysicalFolder, FileName)
            System.IO.File.Delete(PhysicalFolder)
            objGeneric.Success = True
        Catch ex As Exception
            objGeneric.Success = False
            objGeneric.Msg = ex.Message
        End Try
        Return objGeneric.ToString
    End Function

    Public Function RenameFolder(ByVal ParentFolder As String, ByVal FolderName As String, ByVal NewName As String) As String
        Dim objGeneric As New GenericJson
        Try
            Dim PhysicalFolder = GetPhysicalPath(ParentFolder)
            PhysicalFolder = CombinePath(PhysicalFolder, FolderName)
            Dim NewPhysicalPath = GetPhysicalPath(ParentFolder)
            NewPhysicalPath = CombinePath(NewPhysicalPath, NewName)
            System.IO.Directory.Move(PhysicalFolder, NewPhysicalPath)
            objGeneric.Success = True
        Catch ex As Exception
            objGeneric.Success = False
            objGeneric.Msg = ex.Message
        End Try
        Return objGeneric.ToString
    End Function

    Public Function RenameFile(ByVal ParentFolder As String, ByVal FileName As String, ByVal NewName As String) As String
        Dim objGeneric As New GenericJson
        Try
            Dim PhysicalFolder = GetPhysicalPath(ParentFolder)
            PhysicalFolder = CombinePath(PhysicalFolder, FileName)
            Dim NewPhysicalPath = GetPhysicalPath(ParentFolder)
            NewPhysicalPath = CombinePath(NewPhysicalPath, NewName)
            System.IO.File.Move(PhysicalFolder, NewPhysicalPath)
            objGeneric.Success = True
        Catch ex As Exception
            objGeneric.Success = False
            objGeneric.Msg = ex.Message
        End Try
        Return objGeneric.ToString
    End Function

    Public Function GetFolders(ByVal Root As String) As String
        Dim PhysicalFolder = GetPhysicalPath(Root)
        Dim DirInfo As New System.IO.DirectoryInfo(PhysicalFolder)
        Dim Nodes = (From u In DirInfo.GetDirectories _
                    Order By u.Name _
                    Select New TreeNode With {.Cls = "folder", .Id = Root & "/" & u.Name, .Leaf = False, .Text = u.Name}).ToList
        Dim LTree As New Tree
        LTree.Nodes = Nodes
        Dim STree = ToJson(LTree)
        STree = STree.Substring("{""nodes"":".Length)
        STree = STree.Substring(0, STree.Length - 1)
        Return STree
    End Function

    Public Function GetFiles(ByVal Root As String, ByVal Start As Integer, ByVal Limit As Integer, ByVal SortField As String, ByVal Direction As String) As String
        Dim PhysicalFolder = GetPhysicalPath(Root)
        Dim DirInfo As New System.IO.DirectoryInfo(PhysicalFolder)
        Dim objFiles As New JFiles

        objFiles.Results = DirInfo.GetFiles.Count + DirInfo.GetDirectories.Count

        Dim AllList = From d In DirInfo.GetDirectories _
                    Select New JFile With {.Type = "Folder", .FileName = d.Name, .CreationTime = d.CreationTime.ToString, .Size = 0, .Icon = "folder.gif"}

        Dim FileList = (From u In DirInfo.GetFiles _
                       Order By u.Name _
                       Select New JFile With {.Type = u.Extension, .FileName = u.Name, .CreationTime = u.CreationTime.ToString, .Size = u.Length, .Icon = "file.gif"})

        Select Case SortField.ToLower
            Case "filename"
                If Direction.ToUpper = "ASC" Then
                    AllList = AllList.OrderBy(Function(p) p.FileName)
                    FileList = FileList.OrderBy(Function(p) p.FileName)
                Else
                    AllList = AllList.OrderByDescending(Function(p) p.FileName)
                    FileList = FileList.OrderByDescending(Function(p) p.FileName)
                End If
            Case "creationtime"
                If Direction.ToUpper = "ASC" Then
                    AllList = AllList.OrderBy(Function(p) p.CreationTime)
                    FileList = FileList.OrderBy(Function(p) p.CreationTime)
                Else
                    AllList = AllList.OrderByDescending(Function(p) p.CreationTime)
                    FileList = FileList.OrderByDescending(Function(p) p.CreationTime)
                End If
            Case "type"
                If Direction.ToUpper = "ASC" Then
                    AllList = AllList.OrderBy(Function(p) p.FileName)
                    FileList = FileList.OrderBy(Function(p) p.Type)
                Else
                    AllList = AllList.OrderByDescending(Function(p) p.FileName)
                    FileList = FileList.OrderByDescending(Function(p) p.Type)
                End If
            Case "size"
                If Direction.ToUpper = "ASC" Then
                    AllList = AllList.OrderBy(Function(p) p.FileName)
                    FileList = FileList.OrderBy(Function(p) p.Size)
                Else
                    AllList = AllList.OrderByDescending(Function(p) p.FileName)
                    FileList = FileList.OrderByDescending(Function(p) p.Size)
                End If
        End Select

        AllList = AllList.Concat(FileList)
        objFiles.Rows = AllList.Skip(Start).Take(Limit).ToList

        Return ToJson(objFiles)
    End Function


    Private Class JFile

        Private _FileName As String
        Public Property FileName() As String
            Get
                Return _FileName
            End Get
            Set(ByVal value As String)
                _FileName = value
            End Set
        End Property

        Private _CreationTime As String
        Public Property CreationTime() As String
            Get
                Return _CreationTime
            End Get
            Set(ByVal value As String)
                _CreationTime = value
            End Set
        End Property

        Private _Size As Long
        Public Property Size() As Long
            Get
                Return _Size
            End Get
            Set(ByVal value As Long)
                _Size = value
            End Set
        End Property

        Private _Type As String
        Public Property Type() As String
            Get
                Return _Type
            End Get
            Set(ByVal value As String)
                _Type = value
            End Set
        End Property

        Private _Icon As String
        Public Property Icon() As String
            Get
                Return _Icon
            End Get
            Set(ByVal value As String)
                _Icon = value
            End Set
        End Property
    End Class

    Private Class JFiles

        Private _Rows As List(Of JFile)
        Public Property Rows() As List(Of JFile)
            Get
                Return _Rows
            End Get
            Set(ByVal value As List(Of JFile))
                _Rows = value
            End Set
        End Property

        Private _Results As Integer
        Public Property Results() As Integer
            Get
                Return _Results
            End Get
            Set(ByVal value As Integer)
                _Results = value
            End Set
        End Property

    End Class

End Class
